﻿package com.wandian.model
{
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.data.GameData;
	import com.wandian.model.http.RoomData;
	import com.wandian.model.socket.ServerPacketProxy;
	import com.wandian.model.socket.net.Command;
	import com.wandian.model.vo.FriendVo;
	import com.wandian.model.vo.PlayerVo;
	import com.wandian.model.vo.RoomVo;

	import org.puremvc.as3.patterns.proxy.*;
	/**   
	* @author long
	* @QQ 108232706
	* @version 1.0   
	*/   
	public class RoomInfoProxy extends Proxy
	{
		private var _global:GameData;
		public static const NAME:String="RoomInfoProxy";
		private var _serverPoxy:ServerPacketProxy;
		private var _inRoomData:InRoomPlayerDataProxy;

		public function RoomInfoProxy(data:Object=null)
		{
			this._global=GameData.getInstance();

			super(NAME, data);
			return;
		}

		override public function onRegister():void
		{
			trace("RoomInfoProxy onRegister");
			this._serverPoxy=facade.retrieveProxy(ServerPacketProxy.NAME) as ServerPacketProxy;
			this._inRoomData=facade.retrieveProxy(InRoomPlayerDataProxy.NAME) as InRoomPlayerDataProxy;
		}

		public function getRoomInfoByType(param1:String="", param2:String=""):void
		{
			var roomData:RoomData=new RoomData();
			roomData.requestRoomListByType(param2, this);
			return;
		}

		public function responsetRoomInfoByType(param1:Object):void
		{
			var _loc_6:RoomVo=null;
			var _loc_2:Array=[];
			var _loc_3:*=param1.data.aRoomlist as Array;
			var _loc_4:*=_loc_3.length;
			var _loc_5:int=0;
			while (_loc_5 < _loc_4)
			{

				_loc_6=GameData.getRoomInfoById(this._global.minTableType, _loc_3[_loc_5][0]);
				if (_loc_6 == null)
				{
				}
				else
				{
					_loc_6.id=_loc_3[_loc_5][0];
					_loc_6.playerNum=_loc_3[_loc_5][1];
					_loc_6.lookOn=_loc_3[_loc_5][2];
					_loc_2.push(_loc_6);
				}
				_loc_5=_loc_5 + 1;
			}
			this.sendNotification(CLZNoticeName.ROOM_LIST_INFO_SUCC, _loc_2);
			return;
		}

		/**
		 * 在大厅请求房间玩家
		 * @param roomid
		 */
		public function requestRoomPlayerData(roomid:int):void
		{
//            var _loc_2:* = new RoomData();
//            _loc_2.requestRoomPlayerData(param1, this.responseRoomPlayerData);
			this._serverPoxy.writeBegin(Command.CMD_RoomUserList);
			this._serverPoxy.writeByte(roomid);
			this._serverPoxy.writeEnd();
			this._serverPoxy.sendCmd();
			return;
		}

		/**
		 * 设置房间大小盲基础信息
		 */
		public function responseSetRoomInfo():void
		{
			var errId:int=this._serverPoxy.readByte();
			if (errId != 0)
			{
				trace("服务端返回设置错误!");
				return;
			}

			var arrData:Array=[];
			var roomname:String=this._serverPoxy.readString();
			var roomid:int=this._serverPoxy.readInt();
			var seat:int=this._serverPoxy.readInt();
			var minBlind:int=this._serverPoxy.readFloat();
			var maxBlind:int=this._serverPoxy.readFloat();
			var minStake:int=this._serverPoxy.readFloat();
			var maxStake:int=this._serverPoxy.readFloat();
			arrData.push(roomid); //roomid
			arrData.push(roomname); //roomname			
			arrData.push(seat); //seat
			arrData.push(minBlind); //minBlind
			arrData.push(maxBlind); //
			arrData.push(minStake); //minStake
			arrData.push(maxStake);
			arrData.push(0); //playerNum
			this.sendNotification(CLZNoticeName.LOBBY_UPDATEROOMVO, arrData); //设置成功，马上弹快速买入

//			var roomname:String=this._serverPoxy.readString();
//			var roomid:int=this._serverPoxy.readInt();
			if (GameData._objRoom != null && roomid == GameData._objRoom.id)
			{
				GameData._objRoom.name=roomname;
				GameData._objRoom.seat=seat;
				GameData._objRoom.minBlind=minBlind;
				GameData._objRoom.maxblind=maxBlind;
				GameData._objRoom.minStake=minStake
				GameData._objRoom.maxStake=maxStake;
//				GameData._objRoom.name=roomname;
//				GameData._objRoom.seat=this._serverPoxy.readInt();
//				GameData._objRoom.minBlind=this._serverPoxy.readInt();
//				GameData._objRoom.maxblind=this._serverPoxy.readInt();
//				GameData._objRoom.minStake=this._serverPoxy.readInt();
//				GameData._objRoom.maxStake=this._serverPoxy.readInt();
				this.sendNotification(CLZNoticeName.SETROOMINFO_RSP); //设置成功，马上弹快速买入
			}
			else
			{
				trace("服务端返回的房间不是当前房间!");
			}

		}

		/**
		 * 响应大厅房间列表
		 */
		public function responseRoomList():void
		{
			GameData.roomCount=this._serverPoxy.readInt();
			GameData.enableRoomCode=this._serverPoxy.readByte();
			GameData.floorCount=this._serverPoxy.readInt();
			GameData.bettime=this._serverPoxy.readInt();
			var seat:int=this._serverPoxy.readUInt();
			var minBlind:int=this._serverPoxy.readFloat();
			var maxBlind:int=this._serverPoxy.readFloat();
			var minTake:int=this._serverPoxy.readFloat();
			var maxTake:int=this._serverPoxy.readFloat();
			trace("[RoomInfoProxy]获取房间列表,minBlind " + minBlind + " maxBlind " + maxBlind + " seat " + seat + " minTake " + minTake + " maxTake" + maxTake);
			GameData.notEmptyCount=this._serverPoxy.readInt();
			var count:int=0;
			var arrRoom:Array=[];
			var roomVo:RoomVo;
			while (count < GameData.roomCount)
			{
				count++;
				roomVo=new RoomVo();
				roomVo.id=count;
				roomVo.minBlind=minBlind;
				roomVo.maxblind=maxBlind;
				roomVo.minStake=minTake;
				roomVo.maxStake=maxTake;
				roomVo.seat=seat;
				roomVo.playerNum=0;
				roomVo.isvip=0;
				roomVo.close=0;
				roomVo.password="";
				arrRoom.push(roomVo);
			}
			count=0;
			var rid:int=0;
			while (count < GameData.notEmptyCount)
			{
				count++;
				rid=this._serverPoxy.readInt();
				roomVo=arrRoom[rid - 1];
//				roomVo.id=rid;
				roomVo.name=this._serverPoxy.readString();
				roomVo.minBlind=this._serverPoxy.readFloat();
				roomVo.maxblind=this._serverPoxy.readFloat();
				roomVo.minStake=this._serverPoxy.readFloat();
				roomVo.maxStake=this._serverPoxy.readFloat();
				roomVo.seat=this._serverPoxy.readInt();
				roomVo.playerNum=this._serverPoxy.readInt();
//				roomVo.isvip=this._serverPoxy.readInt();
//				roomVo.close=this._serverPoxy.readInt();
//				roomVo.password=this._serverPoxy.readString();
					//						arrRoom.push(roomVo);
			}
			this.sendNotification(CLZNoticeName.LOBBY_ROOMLIST_RSP, arrRoom);
		}

		/**
		 * 响应大厅房间玩家
		 */
		public function responseRoomPlayerData():void
		{
			var friendVo:FriendVo=null;
			var total:int=this._serverPoxy.readInt();
			var arrRoomPlayers:Array=[];
			var count:int=0;
			while (count < total)
			{

				friendVo=new FriendVo();
				friendVo.userid=this._serverPoxy.readInt();
				friendVo.icon=this._serverPoxy.readString();
				friendVo.name=this._serverPoxy.readString();
				friendVo.sex=this._serverPoxy.readByte();
//				friendVo.gold=this._serverPoxy.readInt();
				friendVo.money=this._serverPoxy.readFloat();
				friendVo.state=this._serverPoxy.readByte();
				friendVo.rank=this._serverPoxy.readInt();
				friendVo.level=GameData.getLevel(friendVo.gold);
				arrRoomPlayers.push(friendVo);
				count=count + 1;
			}
			this.sendNotification(CLZNoticeName.SHOW_ROOM_PALYER_LIST_RES, arrRoomPlayers);
			return;
		}

		/**
		 * 收到房间信息变化的广播
		 */
		public function responseRoomUpdate():void
		{
			var errId:int=this._serverPoxy.readByte();
			if (errId != 0)
			{
				trace("responseRoomUpdate服务端返回设置错误!");
				return;
			}

			var arrData:Array=[];
			arrData.push(this._serverPoxy.readInt()); //roomid
			arrData.push(this._serverPoxy.readString()); //roomname			
			arrData.push(this._serverPoxy.readInt()); //seat
			arrData.push(this._serverPoxy.readFloat()); //minStake
			arrData.push(this._serverPoxy.readFloat()); //
			arrData.push(this._serverPoxy.readFloat()); //minStake
			arrData.push(this._serverPoxy.readFloat());
			arrData.push(this._serverPoxy.readInt()); //playerNum
			if (GameData._objRoom != null && GameData._objRoom.id == arrData[0])
			{
				GameData._objRoom.seat=arrData[2];
			}
			this.sendNotification(CLZNoticeName.LOBBY_UPDATEROOMVO, arrData); //设置成功，马上弹快速买入
			return;
		}

		public function responseRoomSeatUpdate():void
		{

			var arrData:Array=[];
			arrData.push(this._serverPoxy.readInt()); //roomid
//			arrData.push(this._serverPoxy.readString()); //roomname			
//			arrData.push(this._serverPoxy.readInt());//seat
//			arrData.push(this._serverPoxy.readInt());//minStake
//			arrData.push(this._serverPoxy.readInt());//
//			arrData.push(this._serverPoxy.readInt());//minStake
//			arrData.push(this._serverPoxy.readInt());
			arrData.push(this._serverPoxy.readInt()); //playerNum
			this.sendNotification(CLZNoticeName.UPDATEROOMPLAYERNUMCHANGE, arrData); //设置成功，马上弹快速买入
			return;
		}

		/**
		 * 请求房间信息
		 * @param roomid
		 */
		public function requestRoomInfo(roomid:int):void
		{

			return;
		}

		/**
		 * 响应房间信息
		 */
		public function responseRoomInfo():void
		{
			this._inRoomData=facade.retrieveProxy(InRoomPlayerDataProxy.NAME) as InRoomPlayerDataProxy;
			var errId:int=this._serverPoxy.readByte();
			if (errId == 0)
			{
				return;
			}
			var objData:Object=new Object();
			objData.dealer=this._serverPoxy.readInt(); //发牌者
			GameData._objRoom.seat=this._serverPoxy.readInt(); //坐位数
			GameData._objRoom.bettime=this._serverPoxy.readByte(); //倒计时
			var publicCardLen:int=this._serverPoxy.readInt(); //牌长
			var arrPublicCard:Array=[]; //牌数组
			var i:int=0;
			while (i < publicCardLen)
			{

				arrPublicCard.push(this._serverPoxy.readString());
				i=(i + 1);
			}
			//发牌消息
			this.getPublicCard(arrPublicCard);
			var bonusPoolLen:int=this._serverPoxy.readInt(); //奖金池长度
			var arrBonusPool:Array=[];
			i=0;
			;
			while (i < bonusPoolLen)
			{

				arrBonusPool.push(this._serverPoxy.readFloat()); //池里押注的金额
				i=(i + 1);
			}
			this.sendNotification(CLZNoticeName.BONUS_POOL_SHOW, arrBonusPool);
			objData.isPlaying=this._serverPoxy.readInt(); //
			objData.curSeatId=this._serverPoxy.readInt();
			objData.time=this._serverPoxy.readInt(); //倒计时
			var playerNum:int=this._serverPoxy.readInt();
			i=0;
			var seatId:int;
			var userid:int;
			var objPlayer:PlayerVo;
			while (i < playerNum)
			{

				seatId=this._serverPoxy.readInt();
				userid=this._serverPoxy.readInt();
				objPlayer=this._inRoomData.getInRoomPlayer(userid);
				objPlayer.seatId=seatId;
				objPlayer.state=this._serverPoxy.readInt();
				objPlayer.lastAction=this._serverPoxy.readInt();
				objPlayer.callChip=this._serverPoxy.readFloat();
				objPlayer.stake=this._serverPoxy.readFloat();
				this.sendNotification(CLZNoticeName.SITDOWN_RSP, objPlayer, "1");
				i=(i + 1);
			}
			this.sendNotification(CLZNoticeName.RESPONSE_ROOM_INFO_SUCC, objData);
			//显示庄家
			this.sendNotification(CLZNoticeName.INIT_DEALER_ID, objData.dealer);		
			return;
		}

		private function getPublicCard(cardArray:Array):void
		{
			if (cardArray == null)
				return;
			var showCard:Object=null;
			if (cardArray.length >= 3) //翻牌
			{
				showCard={card1: cardArray[0], card2: cardArray[1], card3: cardArray[2]};
				this.showSnapCardsFlop(showCard);
			}
			if (cardArray.length >= 4) //转牌
			{
				showCard={card1: cardArray[3]};
				this.showSnapCardsTurn(showCard);
			}
			if (cardArray.length == 5) //河牌
			{
				showCard={card1: cardArray[4]};
				this.showSnapCardsRiver(showCard);
			}
			return;
		}

		//翻牌
		public function showSnapCardsFlop(param1:Object):void
		{
			this.sendNotification(CLZNoticeName.SNAPCARDS_FLOP, param1);
			return;
		}

		//转牌
		public function showSnapCardsTurn(param1:Object):void
		{
			this.sendNotification(CLZNoticeName.SNAPCARDS_TURN, param1);
			return;
		}

		//河牌
		public function showSnapCardsRiver(param1:Object):void
		{
			this.sendNotification(CLZNoticeName.SNAPCARDS_RIVER, param1);
			return;
		}

	}
}
